import 'package:cached_network_image/cached_network_image.dart';
import 'package:demo/app/modules/main/product/product_controller.dart';
import 'package:demo/model/product_model.dart';
import 'package:demo/theme/app_text_theme.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';

import '../../../../theme/app_colors.dart';

class ProductView extends GetView<ProductController> {
  const ProductView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    var isLoadAll = false;
    if(Get.arguments != null){
      isLoadAll = Get.arguments['isLoadAll'];
    }
    print('isLoadAll = $isLoadAll');

    var height = Get.height - MediaQuery.of(context).padding.top - AppBar().preferredSize.height;
    if(isLoadAll){

    }else{
      height -= kBottomNavigationBarHeight;
    }
    if(GetPlatform.isIOS){
      height -= 35; // 不知道为啥
    }
    return Scaffold(
      backgroundColor: Color_bg,
      body: Stack(
        children: [
          getTopBg(context),
          Column(
            children: [
              getTitle(isLoadAll,context),
              RefreshIndicator(
                onRefresh: () => controller.getProduct(),
                child: SizedBox(
                  height: height,
                  child: controller.obx((data) => setItem(data!),
                    onLoading: Center(child: CupertinoActivityIndicator(radius: 15.w)),
                    onEmpty: Center(child: Text('暂无产品',style: text_grey_16b.copyWith(fontSize: 24.sp))), //空数据显示
                    onError: (error) => Center(child: Text('$error',style: text_grey_16b.copyWith(fontSize: 24.sp))), //出错界面显示
                  ),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  Widget getTopBg(BuildContext context){
    return Container(
      width: context.width,
      height: context.width / 2,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [
            Color_main,
            Color(0xfff2f6ff)
          ],
        ),
      ),
    );
  }
  Widget getTitle(bool isLoadAll,BuildContext context) {
    return isLoadAll ?
    AppBar(
      backgroundColor: const Color(0x00000000),
      title: Text('产品列表',style: text_title_white),
      iconTheme: const IconThemeData(color: Color_white),
    )
        :
    Container(
      margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
      height: AppBar().preferredSize.height,
      child: Center(
          child: Text('产品列表', style: text_title_white)
      ),
    );
  }
  setItem(List<Product> data) {
    return ListView.separated(
      padding: EdgeInsets.zero,
      itemCount: data.length,
      separatorBuilder: (BuildContext context, int index) => const Divider(color:Colors.transparent),
      itemBuilder: (BuildContext context, int index) {
        var bean = data[index];
        var min = bean.loanMin >= 10000 ? '${bean.loanMin ~/ 10000}万' : bean.loanMin;
        var max = bean.loanMax >= 10000 ? '${bean.loanMax ~/ 10000}万' : bean.loanMax;
        return InkWell(
          highlightColor : Colors.transparent,
          splashColor: Colors.transparent,
          onTap: () => controller.clickApply(bean),
          child: Container(
            margin: EdgeInsets.only(left: 15.w,right: 15.w,bottom: index == data.length - 1 ? 15.w : 0),
            padding: EdgeInsets.symmetric(horizontal: 12.w,vertical: 12.w),
            decoration: BoxDecoration(
              color: Color_white,
              borderRadius: BorderRadius.all(Radius.circular(8.w)),
            ),
            child: Column(
              children: [
                Row(children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(6),
                    child: CachedNetworkImage(
                      width: 35.w, height: 35.w,
                      imageUrl: bean.logo,
                      placeholder: (context, url) => Container(padding:EdgeInsets.all(10.w), child: const CircularProgressIndicator(color: Color_grey)),
                      errorWidget: (context, url, error) => Container(color: Color_bg),
                    ),
                  ),
                  SizedBox(width: 12.w),
                  Text(bean.name,style: text_131732_16)
                ]),
                SizedBox(height: 12.w),
                Row(
                    mainAxisAlignment : MainAxisAlignment.spaceBetween,
                    crossAxisAlignment : CrossAxisAlignment.end,
                    children: [
                      Container(
                        child: Column(
                            crossAxisAlignment : CrossAxisAlignment.start,
                            children: [
                              Text('$min~$max',style: text_white_24b.copyWith(color: const Color(0xffFF6666))),
                              Text('额度范围（元）',style: text_grey_14)
                            ]),
                      ),
                      Container(
                        child: Column(
                            crossAxisAlignment : CrossAxisAlignment.start,
                            children: [
                              // Text(bean.loanSpeed,style: text_131732_16),
                              // Text('放款速度',style: text_grey_14)
                              Text('${bean.interestRate}',style: text_131732_16),
                              Text(bean.interestMethod=='day'?'日利率':'月利率',style: text_grey_14)
                            ]),
                      ),
                      TextButton(
                          style: TextButton.styleFrom(
                              padding: EdgeInsets.zero,
                              minimumSize : Size(60.w, 25.w),
                              maximumSize : Size(60.w, 25.w),
                              backgroundColor: Color_main,
                              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.w))
                          ),
                          onPressed: () => controller.clickApply(data[index]),
                          child: Text('申请',style: text_white_14)
                      ),
                    ])
              ],
            ),
          ),
        );
      },
    );
  }


}